package com.box.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.box.service.WeiXinService;
import com.box.utils.ResponseResult;

@Controller
@RequestMapping("/WXpay")
public class WeiXinController {

	
	@Autowired
	private WeiXinService wxService;
	
	@RequestMapping(value="/notifyUrl",method={RequestMethod.GET,RequestMethod.POST})	
	public String notifyUrl(HttpServletRequest request,HttpServletResponse response,Model model) throws Exception{
		
		try {
			String payNotifyUrl = wxService.PayNotifyUrl(request, response);
			model.addAttribute("msg", payNotifyUrl);
			return "WXpay/notifyUrl";
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			return "error/404";
		}
		
	}
	
	@RequestMapping(value="/loadPayState",method={RequestMethod.GET,RequestMethod.POST})	
	@ResponseBody
	public ResponseResult returnUrl(HttpServletRequest request,HttpServletResponse response,Model model) throws Exception{
		 response.setHeader("Access-Control-Allow-Origin", "*");
				return  wxService.LoadPayStatus(request, response);
		
	}
	
	
	@RequestMapping(value="/order",method={RequestMethod.GET,RequestMethod.POST})	
	public String GoOrderPage(@RequestParam("orderNum") String orderNum,Model model) throws Exception{
							String result = wxService.getOrderListByOrderNum(orderNum);
							System.out.println(result);
							model.addAttribute("result", result);
				return  "Wx/QueryOrderResult";
		
	}
}
